package net.kunyv.base.service;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import net.kunyv.base.entity.SysUser;
import net.kunyv.base.mapper.SysUserMapper;
import net.kunyv.util.Constant;
import net.kunyv.util.HdUtil;
import net.kunyv.util.PageParam;
import net.kunyv.util.PageResult;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 用户servcie
 *
 * @author sunyb
 * @date 2017-08-02
 */
@Service
public class SysUserService extends BaseService<SysUser> {

    Logger logger = LoggerFactory.getLogger(SysUserService.class);
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 根据用户名获取用户信息
     *
     * @param userName
     * @return net.kunyv.kauth.entity.SysUser
     * @throw
     * @author sunyb
     * @date 2017/8/2
     */
    public SysUser getUserByUserName(String userName) {
        Example example = new Example(SysUser.class);
        example.createCriteria().andEqualTo("userName", userName);
        List<SysUser> userList = sysUserMapper.selectByExample(example);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList.get(0);
        }
    }


    /**
     * 获取用户列表
     * sunyb
     * 2018/12/7
     */
    public PageResult getUserList(String userName, PageParam pageParam) {
        Example example = new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(userName)) {
            criteria.andLike("userName", "%" + userName + "%");
        }
        example.setOrderByClause("user_name desc");
        //判断数据权限
        Page p = PageHelper.startPage(pageParam.getPage(), pageParam.getRows());
        List<SysUser> l = sysUserMapper.selectByExample(example);
        PageResult result = new PageResult();
        result.setRows(l);
        result.setTotal(p.getTotal());
        return result;
    }


    /**
     * 保存用户
     * sunyb
     * 2020/8/28
     */
    public String saveUser(SysUser sysUser) {

        SysUser loginUser = HdUtil.getLoginUser();
        int result;
        if (sysUser.getUserId() != null && !sysUser.getUserId().equals("")) {
            sysUser.setUpdateTime(new Date());
            sysUser.setUpdateUser(loginUser.getUserName());
            result = sysUserMapper.updateByPrimaryKeySelective(sysUser);
        } else {
            sysUser.setUserId(null);
            sysUser.setUpdateUser(loginUser.getUserName());
            sysUser.setCreateUser(loginUser.getUserName());
            sysUser.setUpdateTime(new Date());
            sysUser.setCreateTime(new Date());
            result = sysUserMapper.insertSelective(sysUser);
        }
        if (result == 1) {
            return "success";
        } else {
            return Constant.SAVE_FAIL_INFO;
        }
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return java.lang.String
     * @throw
     * @author sunyb
     * @date 2020/8/28
     */
    public String deleteUserByUserId(String userId) {
        String result = "success";
        try {
            Example example = new Example(SysUser.class);
            example.createCriteria().andEqualTo("userId", userId);
            //删除用户
            sysUserMapper.deleteByPrimaryKey(userId);
        } catch (Exception e) {
            result = Constant.DELETE_FAIL_INFO;
            logger.error("删除用户失败", e);
        }
        return result;
    }
}